package cn.kailangtu.myapplication.camera;

import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;
import android.view.SurfaceHolder;
import android.widget.TextView;

import androidx.annotation.NonNull;



public class AvcPlayerHelper {
    String TAG = "AvcPlayerHelper";
    private SurfaceHolder surfaceHolder;
    private AvcPlayer avcPlayer;
    SmIpCamera smIpCamera;
    private int RTSP_MS = 0;
    private boolean isOpen = false;
    private String urlRTSP = "rtsp://192.168.1.10:8554/profile0";
    private TextView tvConnectMsg = null;

    private Activity context = null;


    public void setTvConnectMsg(TextView textView)
    {
        this.tvConnectMsg = textView;
    }

    public void initIPCameraView(AvcPlayer avcPlayer,String url,Activity context){
        this.avcPlayer = avcPlayer;
        this.urlRTSP = url;
        this.context = context;
        surfaceHolder = avcPlayer.getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                Log.d(TAG,"surfaceCreated");
                cshAVCSmail1();
            }
            @Override
            public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                Log.d(TAG,"surfaceChanged");
            }
            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
                Log.d(TAG,"surfaceDestroyed");
                avcPlayer.stopPreview();
            }
        });

    }

    private void cshAVCSmail1(){
        Log.d(TAG,"cshAVC1");

        avcPlayer.init(avcPlayer.getWidth(), avcPlayer.getHeight(), new AvcPlayer.Callback() {
            @Override
            public void onFrameUpdate(byte[] frame) {
            }
        });
        avcPlayer.startPreview();
        smIpCamera = new SmIpCamera("1", urlRTSP, RTSP_MS, 20, avcPlayer);
        openCamera();
    }

    private void openCamera(){
        if(smIpCamera != null) {
            //showSmDialog("正在打开2号相机...");
            Log.d(TAG, "openCamera1");
            smIpCamera.openCamera(new SmIpCamera.CallbackIpcCamOpen() {
                @Override
                public void onOpenSuc(int errCode) {
                    isOpen = true;
                    spy();
                }

                @Override
                public void onOpenFai(int errCode) {
                }
            });
        }
    }

    public void pause(boolean isPause)
    {
        if(smIpCamera != null) {
            smIpCamera.setPause(isPause);
        }

    }

    public void close(){
        if(isOpen)
        {
            isOpen = false;
            SystemClock.sleep(100);
            smIpCamera.CloseCamera();

        }
    }

    boolean isReOpen = false;
    public void reOpen(){
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                openCamera();
                isReOpen = true;
                /*
                if(tvConnectMsg != null)
                {
                    tvConnectMsg.setVisibility(TextView.VISIBLE);
                }

                 */
            }
        });

    }

    public void closeMsg()
    {
        if(isReOpen)
        {
            isReOpen = false;
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(tvConnectMsg != null) {
                        tvConnectMsg.setVisibility(TextView.GONE);
                    }
                }
            });
        }
    }

    boolean isSpy = false;
    public  void spy()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(isSpy == false){
                    isSpy = true;
                    while(isSpy && isOpen)
                    {
                        if((System.currentTimeMillis() - SmIpCamera.lastReceiveTime) > 2000)
                        {
                            SmIpCamera.lastReceiveTime = System.currentTimeMillis();
                            reOpen();
                            //EventBus.getDefault().post(new CameraReopenEvent());
                        }
                        else{
                            //closeMsg();
                        }

                        SystemClock.sleep(1000);
                    }
                }
            }
        }).start();

    }
}
